Skip to content

推理设备说明 - InferenceDevice

加载 YOLO 模型时的 inferenceDevice 参数,与 YoloListDevices 返回的 DeviceIndex 使用同一套编号(Windows 显卡顺序)。各后端在库内自行映射到 Vulkan / CUDA,调用方无需再换算。


取值约定

inferenceDevice含义
-1CPU。NCNN / ONNX 可用;TensorRT 不支持 CPU,传 -1 会加载失败。
012对应 YoloListDevicesDevices[]Type≠CPUDeviceIndex(Windows 显卡顺序)。

推荐流程:先调用 YoloListDevices,在统一的 Devices 数组里按 Name / Type / SupportedBackends 选设备,再把该条目的 DeviceIndex 原样传给 YoloLoadModel / YoloLoadModelEx 等接口。


后端差异

后端 (modelType)设备映射非 NVIDIA 卡传 GPU 索引
NCNN(2)DeviceIndex → Vulkan 设备可用(如 Intel 核显 Vulkan);部分机器核显不稳定,失败时可能回退 CPU
ONNX(1)DeviceIndex → CUDA(仅 Type=NVIDIA加载失败(明确报错);CPU 请传 -1
TensorRT(0)DeviceIndex → CUDA(仅 Type=NVIDIA加载失败;必须传 NVIDIA 的 DeviceIndex

列表字段 CudaDeviceIndex 为库内推算的 NVIDIA 序号(供对照),不要把它当作 inferenceDevice 传入。SupportedBackends 标明该卡可用后端:Ncnn / TensorRt / Onnx


典型双显卡示例

text
DeviceIndex -1  Name=CPU                  Type=CPU     SupportedBackends=["Ncnn","Onnx"]
DeviceIndex  0  Name=Intel UHD Graphics   Type=Intel   SupportedBackends=["Ncnn"]
DeviceIndex  1  Name=NVIDIA GeForce RTX   Type=NVIDIA  CudaDeviceIndex=0  SupportedBackends=["Ncnn","TensorRt","Onnx"]
传参NCNNTensorRT / ONNX CUDA
1独显独显(内部 CUDA0)
0核显创建模型失败(非 NVIDIA)
-1CPUONNX→CPU;TensorRT 不允许

与 ModelInfo 的关系

加载成功后,YoloGetModelInfo 中:

  • InferenceDeviceIndex / RequestedInferenceDeviceIndex:对外设备索引(GPU 时为 Windows 显卡顺序)
  • InferenceDevice:可读标签,如 CPUGPU0GPU1
  • NCNN 还可能带 SelectedVulkanDeviceIndexSelectedWindowsGpuIndex 等诊断字段

GPU 不可用时,部分路径会回退 CPU,见 ProviderFallbackReason / ExecutionProvider


多 CPU

YOLO 不会在多个物理 CPU / 插槽之间做设备选择。inferenceDevice=-1 表示使用主机 CPU,由操作系统在可用核心上调度线程。见 YoloListDevicesDevices[]Type=CPU 的条目(通常带 Note)。